home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / TAWUG / TAWUG Disk No. 73 (SHK) / TAWUG73.shk / ASSEMBLY.SUPP (.txt) < prev    next >
AppleWorks Document  |  1988-07-12  |  7KB  |  103 lines

  1. O=====|====|====|====|====|====|====|====|====|====|====|====|====|====|====|===
  2. ASSEMBLY CLASS SUPPLEMENT
  3. DThis is an answer to a question raised by Ed Behrens concerning the H
  4. Fassembly language class. It is a rather lengthy post so forewarned is M
  5. Kfour-armed. Although the subject of his question is a bit beyond the scope K
  6. Iof lesson one, it feeds nicely into lesson four. If you don't understand J
  7. Hwhat is going on now, don't panic. Save it, read lesson four, then come 
  8. back to it.
  9. IFor those of you downloading the assembly language class, in session one I
  10. Gyou will remember that I included a simple program written in assembly M
  11. Klanguage. All it did was wait for a keypress, then print an number of dots L
  12. Jcorresponding to the hexadecimal value of the keypress. The program looks 
  13. like this:
  14.                 1    ********************************0
  15.                 2    * Another useless program?
  16.                 3    * But a fun example of assembly language7
  17.                 4    ********************************
  18.                 57
  19.                 6    *-- Equates --------------------D
  20.                 7    COUT     EQU   $FDED      ;output a characterB
  21.                 8    BELL     EQU   $FBDD      ;beep the speaker
  22.                 9    STROBE   EQU   $C010      ;keyboard flags:
  23.                 10   KEY      EQU   $C000      ;keyboard
  24.                 117
  25.                 12   *-- The program ----------------D
  26. 8000: 2C 10 C0  13            BIT   STROBE     ;clear the keyboard)
  27. 8003: AD 00 C0  14   GETKEY   LDA   KEYE
  28. 8006: 10 FB     15            BPL   GETKEY     ;wait for a keypress>
  29. 8008: 29 0F     16            AND   #$0F       ;ascii to hexC
  30. 800A: AA        17            TAX              ;save the keypress*
  31. 800B: A9 AE     18            LDA   #"."E
  32. 800D: 20 ED FD  19   PRINT    JSR   COUT       ;print "." on screen#
  33. 8010: CA        20            DEX+
  34. 8011: D0 FA     21            BNE   PRINT@
  35. 8013: 20 DD FB  22            JSR   BELL       ;sound the bell:
  36. 8016: 60        23            RTS              ;and quit
  37. FI will refer to the program by line number (#1-#23) but remember that L
  38. Jthese numbers do not hold the same significance as line numbers in BASIC. =
  39. They are only there for the human programmer's convenience.
  40. KNow...the question that Ed asked concerns was; "How does BNE PRINT get you 
  41. to the address $800D?"
  42. KFirst some explanations. Look in the far left two columns for lines 19, 20 
  43. and 21. They read:
  44. 800D: 20 ED FD  19
  45. 8010: CA        20
  46. 8011: D0 FA     21
  47. What does this mean?
  48. JLine 19: At address $800D, the value $20 is found. This is an instruction H
  49. Fto the 65C02 to Jump to a Subroutine at the address pointed to by the M
  50. Kfollowing two bytes of information. These bytes are listed as "ED FD." The 
  51. actual "address" is $FDED.
  52. KHEY- WHAT GIVES? How does "ED FD" equal $FDED? Sorry. Those fine folks who L
  53. Jdesigned the 6502 decided that it would be easier to reverse the "lo" and G
  54. E"hi" bytes of addresses. Therefore, whenever you see an address that J
  55. refers to a location in memory, it will be chopped in half and reversed.
  56. HLine 20: Location $8010 contains the value $CA, which is "65C02ese" for M
  57. KDEX or Decrement the value in the X register by one. Sort of like "LET X = 
  58. X-1."
  59. GLine 21: Location $8011 equals $D0, the code for BNE, or Branch if not K
  60. Iequal to. During the operation of BNE, our friend the 65C02 looks at the M
  61. K"Zero" flag (a mysterious "thing" that we will talk about someday.) If the L
  62. JZero flag is indicating that the result of the previous operation was NOT M
  63. Kequal to zero, BNE says that the program should branch to the line labeled J
  64. H"PRINT" (line 19). BNE is equivalent to the BASIC statement, "IF Z <> 0 
  65. THEN GOTO PRINT." 
  66. KBut how does it know to branch to PRINT. The only other byte in line 21 is J
  67. H"$FA." Somehow, this tells the 65C02 where to branch to. It can't be an L
  68. Jaddress in memory, addresses are always two bytes (e.g. $FDED). Actually, H
  69. Fit is a "Distance." The second byte in a branch instruction tells the M
  70. K65C02 how far to jump, in bytes. It can also tell which direction to jump, K
  71. Iforward or backward. Forward or backward is indicated by the "Hi" bit of H
  72. Fthe byte. In binary, the hi bit equaling one looks like this - "%1000 H
  73. F0000". If the hi bit is set, as in our example, the 65C02 will branch H
  74. Fbackwards from the current position in memory. If the hi bit is clear <
  75. (equal to zero), the 65C02 will branch forwards in memory.
  76. Take line 19. The instruction codes are:*
  77.    $D0 - BNE (Branch if not equal to...)/
  78.    $FA - Binary representation is %1111 1010.M
  79. KHi bit is equal to 1, therefore branch backwards in memory. Bits 6 through L
  80. J0 equal "111 1010", which equals $7A in hex. This value indicates how far M
  81. Kback to branch. Notice that I did not say, "This value EQUALS how far back K
  82. Ito branch." If the 65C02 moved back $7A bytes (122 in decimal), it would M
  83. Kbranch past the beginning of the program. Instead, it performs some tricky 
  84. math with it.G
  85. E  1) Subtract the offset value from $FF ($FF - $FA). In our case the 
  86. difference is $05.K
  87. I  2) Subtract the difference ($05) from the current value of the PROGRAM L
  88. JCOUNTER. This counter points to the last byte read, which in our case, is F
  89. Dthe offset. This value is at location $8012. Therefore, the program 0
  90. counter equals $8012. So, $8012 - $05 = $800D.M
  91. K  3) The result goes into the program counter, which then indicates to the J
  92. 65C02 that the next byte should be read from location $800D. HEY PRESTO!
  93. EClear as mud. Now do you see the reason some genius came up with the L
  94. Jassembler? Assemblers figure out this stuff automatically. All we have to C
  95. do is indicate where to branch to, and the program does the rest.
  96. IIncidentally, notice that a branch can only happen to a limited range of J
  97. Haddresses. Because we are only allowed to use the low seven bits of the J
  98. Hbyte indicating the offset, we can only branch 127 bytes forward or 128 J
  99. Hbytes backward. Trying to go further than this results in the annoying, G
  100. E"BRANCH OUT OF RANGE!" At this point, the programmer needs to fake a ;
  101. decisional jump, but that is entirely ahead of ourselves.
  102. Hope this helps! More later. --- mnr
  103.